/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pushy.client;

import java.util.LinkedList;
import javax.swing.JOptionPane;
import pushy.server.Level;
import pushy.server.Pushy;

/**
 *
 * @author dmeyer5
 */
public class Client extends ZAKlassen.Client {
    //Startbildschirm start;
    LinkedList<String> nachrichtenschlange = new LinkedList<>();
    private GUI gui;
    String username;
    String color = "gelb";
    Level level;

    /*public Client(String pIPAdresse, int pPortNr, Startbildschirm s) {
        super(pIPAdresse, pPortNr);
        start = s;
    }*/
    public Client(String pIPAdresse, int pPortNr, GUI gui) {
        super(pIPAdresse, pPortNr);
        this.gui = gui;
    }
    
    public String getColor() {
        return color;
    }
    
    @Override
    public void processMessage(String pMessage) {
       System.out.println("<<" + pMessage);

       if(pMessage.contains("REQUEST")) {
           
       /*    String[] text= pMessage.split(":");
           start.anfrage(text[1]);
       }else if(pMessage.contains("ACCEPTED")) {
           start.spielStarten();
       }else if(pMessage.contains("LEVEL:")) {
           start.getSteuerung().updateFromString(pMessage.replace("LEVEL:", ""));*/
           //gui.Benachrichtigung(pMessage.replace("REQUEST:", ""));
           int reply = JOptionPane.showConfirmDialog(null, "Du hast eine Anfrage von " + pMessage.replace("REQUEST:",""));
           boolean b = (JOptionPane.YES_OPTION == reply);
           if(b) {
               senden("ACCEPTED:" +pMessage.replace("REQUEST:", ""));
           }else {
               senden("REFUSED:" +pMessage.replace("REQUEST:", ""));
           }
       }else if(pMessage.contains("ACCEPTED")) {
           if(gui.getPanel() instanceof Challenge) ((Challenge) gui.getPanel()).challengeResponse(true);
            gui.Levelansicht();
       }else if(pMessage.contains("REFUSED")) {
           ((Challenge) gui.getPanel()).challengeResponse(false);
       }else if(pMessage.contains("LEVEL:")) {
           getLevel().updateFromString(pMessage.replace("LEVEL:", ""));
           gui.getPanel().repaint();
           //System.out.println(getLevel().toString());
       }else if(pMessage.contains("COLOR:")) {
           color = pMessage.replace("COLOR:", "");
            if(color.equals("grün")) {
                ((Levelansicht) gui.getPanel()).level.setGreen(username);
                ((Levelansicht) gui.getPanel()).level.setYellow("mitspieler");
            }else {
                ((Levelansicht) gui.getPanel()).level.setYellow(username);
                ((Levelansicht) gui.getPanel()).level.setGreen("mitspieler");
            }
       }else if(pMessage.contains("MOVE:")) {
           getLevel().getPushy("mitspieler").bewegen(pMessage.replace("MOVE:", ""));
           checkGewonnen();
           gui.getPanel().repaint();
       }else {
           nachrichtenschlange.add(pMessage);
       }
    }
    
    public pushy.server.Level getLevel() {
        if(gui.getPanel() instanceof Levelansicht) {
            level = gui.getLevelansicht().level;
            return level;
        }
        return level;
    }
    
    public GUI getGUI() {
        return gui;
    }
    
    public void senden(String pMessage) {
        System.out.println(">>" + pMessage);
        send(pMessage);
    }
    
    public String getMessage() {
        if(nachrichtenschlange.size() == 0) return null;
        System.out.println(nachrichtenschlange.peek());
        return nachrichtenschlange.pop();
    }            
    
    public String peekMessage() {
        return nachrichtenschlange.peek();
    }

    public boolean waitFor(String s) {
        if(peekMessage() != null) {
            return peekMessage().contains(s);
        }
        return false;
    }
    
    public boolean login(String username) {
        senden("USER:" + username);
        while(peekMessage() == null) {}
        String message = getMessage();
        this.username = username;//ToDo:Nur wenn angemeldet!
        return message.equals("OK");
    }
    
    public boolean register(String username) {
        senden("NEW_USER:" + username);
        while(peekMessage() == null) {}
        String message = getMessage();
        this.username = username;//ToDo:Nur wenn registriert!
        return message.equals("OK");
    }
    
    public boolean logout() {
        if(istVerbunden()) {
            senden("CLOSE");
            return true;
        }
        return false;
    }
    
    public String onlinelist() {
        senden("ONLINE");         
        while(peekMessage() == null) {}
        String[] online = getMessage().split(":");
        String str = "";
        for (String online1 : online) {
            if (!username.equals(online1)) {
                str = str + online1 + "\n";
            }
        }
        return str;
    }
    
    public void challenge(String username) {
        senden("REQUEST:" + username);
    }
    
    public void movement(String movement) {
        pushy.server.Level l = getLevel();
        Pushy p = l.getPushy(username);
        senden("MOVE:" + movement);
        p.bewegen(movement);
        checkGewonnen();
        gui.getContentPane().repaint();
    }
    
    public boolean checkGewonnen() {
        if(getLevel().zielBetreten()) {
            JOptionPane.showMessageDialog(null, "Du hast gerade ein Level geschafft ^^");
            senden("LEVEL_ENDED");
            logout();
            System.exit(0);
            return true;
        }
        return false;
    }
}
